package me.passtime.projt.reg;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import me.passtime.projt.R;
import me.passtime.projt.util.AlertDialogUtils;
import me.passtime.projt.util.CheckUtils;
import me.passtime.projt.util.PageJumpUtils;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-5
 * Time: 下午3:37
 */
public class RegisterStepOnePage extends Activity implements View.OnClickListener {
    private EditText mPhoneNumber;
    private EditText mPassword;
    private EditText mRepeatPwd;
    private Handler mHandler;
    private RegisterController mController;
    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reg_stepone);
        mPhoneNumber=(EditText)this.findViewById(R.id.reg_phone_number);
        mPassword=(EditText)this.findViewById(R.id.reg_password);
        mRepeatPwd=(EditText)this.findViewById(R.id.reg_rp_pwd);
        this.findViewById(R.id.reg_home_btn).setOnClickListener(this);
        this.findViewById(R.id.reg_clean_btn).setOnClickListener(this);
        this.findViewById(R.id.verification_code_btn).setOnClickListener(this);
        mHandler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    default:
                        break;
                }
            }
        };
        mController=new RegisterController(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.reg_home_btn:
                PageJumpUtils.logoutAndJumpToMainPage(this);
                break;
            case R.id.reg_clean_btn:
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mPhoneNumber.setText("");
                    }
                });
                break;
            case R.id.verification_code_btn:
                requestVerificationCode();
                break;
        }
    }
    @Override
    public void onBackPressed() {
        PageJumpUtils.logoutAndJumpToMainPage(this);
    }

    public void requestVerificationCode() {
        String phoneNumber=mPhoneNumber.getText().toString().trim();
        String password=mPassword.getText().toString().trim();
        String repeatPwd=mRepeatPwd.getText().toString().trim();
        if("".equals(phoneNumber) || "".equals(password) || "".equals(repeatPwd)){
            AlertDialogUtils.showConfirmDiaLog(this,"请完整填写.");
            return;
        }
        if (!CheckUtils.isMobileNo(phoneNumber)) {
            AlertDialogUtils.showConfirmDiaLog(this, "请输入正确的手机号");
            return;
        }
        if(!password.equals(repeatPwd)){
            AlertDialogUtils.showConfirmDiaLog(this,"两次密码不一致.");
            return;
        }
        showDialog(DIALOG_PROGRESS);
        mController.sendVerificationCode(phoneNumber,password);
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
